package com.ruoyi.web.controller.customer;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.CustomerAddress;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.CustomerAddressMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: ruohao
 * @Date: 2023/9/21
 **/
@Api("用户送货地址")
@RestController
@RequestMapping("/customer/address")
public class CustomerAddressController extends BaseController {
    @Resource
    private CustomerAddressMapper customerAddressMapper;

    @ApiOperation("用户送货地址列表")
    @Log(title = "查询人员管理", businessType = BusinessType.OTHER)
    @GetMapping("/list")
    public TableDataInfo list(CustomerAddress customerAddress)
    {
        customerAddress.setCustomerId(SecurityUtils.getUserId());
        List<CustomerAddress> list = customerAddressMapper.selectList(customerAddress);
        return getDataTable(list);
    }



    @ApiOperation("新增用户送货地址")
    @Log(title = "新增人员管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody CustomerAddress customerAddress) {
        customerAddress.setCustomerId(SecurityUtils.getUserId());
        int id = customerAddressMapper.insert(customerAddress);
        if ("默认".equals(customerAddress.getType())) {
            onlyDefault(customerAddress.getCustomerId(), (long) id);
        }
        return success();
    }

    @ApiOperation("修改用户送货地址")
    @Log(title = "修改人员管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CustomerAddress customerAddress) {
        customerAddressMapper.update(customerAddress);
        if ("默认".equals(customerAddress.getType())) {
            onlyDefault(customerAddress.getCustomerId(), customerAddress.getId());
        }
        return success();
    }

    private void onlyDefault(Long customerId, Long defaultId) {
        List<CustomerAddress> customerAddresses = customerAddressMapper.selectList(CustomerAddress.builder().customerId(customerId).build());
        if (CollectionUtils.isEmpty(customerAddresses)) {
            return;
        }
        for (CustomerAddress address : customerAddresses) {
            if (defaultId.equals(address.getId())) {
                continue;
            }
            CustomerAddress update = new CustomerAddress();
            update.setId(address.getId());
            update.setType("普通");
            customerAddressMapper.update(update);
        }
    }

    @ApiOperation("删除用户送货地址")
    @Log(title = "删除人员管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        customerAddressMapper.deleteById(id);
        return success();
    }
}
